package org.wlgzs.shop.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wlgzs.shop.bean.*;
import org.wlgzs.shop.dao.CarDataMapper;
import org.wlgzs.shop.service.ICarDataService;
import org.wlgzs.shop.service.IGoodsDateService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service(value = "carService")
public class CarService  implements ICarDataService {
    @Resource
    CarDataMapper carDataMapper;

    @Autowired
    IGoodsDateService iGoodsDateService;


    //添加购物车
    @Override
    public Msg addCar(CarData data) {
        CarData carData=selectOneCarDate(data.getPayment(),data.getStoreName());
        if (carData!=null){
            int number=carData.getNum();
            System.out.println(number+"=====================");
            number+=data.getNum();
            System.out.println(number+"=====================");
            carData.setNum(number);
            carDataMapper.updateByPrimaryKeySelective(carData);
            return Msg.success();
        }
        int number1=carDataMapper.insert(data);
        if(number1!=0){
            return Msg.success().add("win","购物车添加成功！");
        }
        else{
            return Msg.fail().add("errors","添加购物车失败！");
        }
    }

    //删除购物车
    @Override
    public Msg deleteCar(String id,int userId) {
        CarDataExample carDataExample=new CarDataExample();
        CarDataExample.Criteria criteria=carDataExample.createCriteria();
        criteria.andPaymentEqualTo(id);
        criteria.andStoreNameEqualTo(userId+"");
        carDataMapper.deleteByExample(carDataExample);
        return Msg.success();
    }


    //查询用户购物车
    @Override
    public List<ShowDate> carList(int userId) {
        CarDataExample carDataExample=new CarDataExample();
        CarDataExample.Criteria criteria=carDataExample.createCriteria();
        System.out.println(userId+"======================");
        criteria.andStoreNameEqualTo(userId+"");
        List<CarData> list=carDataMapper.selectByExample(carDataExample);
        List<ShowDate> showDateList=new ArrayList<ShowDate>();
        for(CarData data:list){
            ShowDate showDate=new ShowDate();
            GoodsData goodsData=iGoodsDateService.selectGoods(Integer.parseInt(data.getPayment()));
            showDate.setPhotoName(goodsData.getPhotoUrl());
            showDate.setName(goodsData.getGoodsName());
            showDate.setId(goodsData.getId());
            showDate.setPrice(goodsData.getGoodsPrice());
            showDate.setNumber(data.getNum());
            showDate.setCarId(data.getOrderId());
            showDateList.add(showDate);
        }
        return showDateList;
    }


    //模糊查询购物车
    @Override
    public List<CarData> selectCar(String Date) {
        return null;
    }

    //搜索购物车
    public CarData selectOneCarDate(String goodsId,String userId){
        CarDataExample carDataExample=new CarDataExample();
        CarDataExample.Criteria criteria=carDataExample.createCriteria();
        criteria.andPaymentEqualTo(goodsId);
        criteria.andStoreNameEqualTo(userId);
        List list=carDataMapper.selectByExample(carDataExample);
        if(list!=null&&list.size()!=0){
            return (CarData) list.get(0);
        }
        return null;
    }

    @Override
    public void upDateCarNumber(CarData data){
        carDataMapper.updateByPrimaryKeySelective(data);
    }

    @Override
    public List<CarData> selectCar(int userId) {
        CarDataExample carDataExample=new CarDataExample();
        CarDataExample.Criteria criteria=carDataExample.createCriteria();
        criteria.andStoreNameEqualTo(userId+"");
        return carDataMapper.selectByExample(carDataExample);
    }

}
